<?php
//先设置命名空间为后台，控制器
namespace Admin\Controller;
//使用Think文件夹里面的Controller类，并一定要继承controller，才可使自定义控制正常使用

use Think\Controller;
use Common\Model\BaseModel;
use Common\Model\AreaModel;
class CityController extends CommonController{



//************************总区域信息*********************************


    /**
     *  显示所有的城市信息
     *
     **/
    function index(){
        $area = M('city');
        $where = '';
        $count=$area->where($where)->count();
        $limit = 10;
        $page=new \Org\Page($count,$limit);
        $a =$area
            ->field('aga.id,aga.city,aga.is_show,ag.name as aname')
            ->alias('aga')
            ->where($where)
            ->join('__AREA__ ag ON aga.aid=ag.id','LEFT')
            ->order('aga.id desc')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();


        $show=$page->show();
        $title['title'] = '所有城市';
        $title['add'] = '添加新的城市';
        $title['url'] = 'City/create';
        $this->assign('title',$title);
        $this->assign('list',$a);
        $this->assign('show',$show);
        $this->display();
    }


    /**
     *    创建新的城市信息
     *
     */
    public function create(){
        $title['title'] = '城市添加';
        $title['url'] = 'City/add';
        $where['is_show'] = '1';
        $sort = 'id desc';
        $area = D('area')->where($where)->order($sort)->select();
        $this->assign('title',$title);
        $this->assign('area',$area);
        $this->display();
    }


    /**
     *   添加新的区域
     *
     */
    public function add(){
        $data=I('post.');
        unset($data['id']);

        $result= D('city')->add($data);
        if ($result) {
            $this->success('添加成功',U('Admin/City/index'));
        }else{
            $this->error('添加失败');
        }
    }

    /**
     *   删除区域
     *
     **/
    public function delete(){
        if(!IS_AJAX) {
            $this->ajaxReturn(array(
                'info' => '非法的请求方式'
            ));
        }

        $id = $_POST['id'];
        if (empty($id)) {
            $this->ajaxReturn(array(
                'info' => '删除的内容不存在'
            ));
        }

        $result = D('city')->delete($id);
        if ($result > 0) {
            $data = array(
                'info' => 'ok',
            );
        } else {
            $data = array(
                'info' => '删除失败'
            );
        }
        $this->ajaxReturn($data);
    }


}
